﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using ArcannEngine.Entities;
using ArcannEngine.Components;
using ArcannEngine.GeometricPrimitives;

namespace ArcannEngine.Scene
{
    public class SceneManager : Manager
    {
        // Słownik przechowujący encje i odpowiadające im klucze-stringi
        private Dictionary<String, Entity> entities;
        public RocketEntity rocket;
        public List<HeightMapInfo> heightMaps = new List<HeightMapInfo>();
        // Teren
        HeightMapInfo heightMapInfo;
        public HeightMapInfo HeightMapInfo
        {
            get { return heightMapInfo; }
        }

        public Dictionary<String, Entity> Entities
        {
            get { return entities; }
        }

        public SceneManager(RocketGame Game)
            : base(Game)
        {
            this.entities = new Dictionary<String, Entity>();
            Initialize();
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        public ContentManager Content
        {
            get { return Game.Content; }
        }

        protected override String GetName()
        {
            return "Scene";
        }

        public override void LoadContent()
        {
            Entity terrain = new Entity(this, "terrain");
            terrain.position = new Vector3(0, 0, 0);
            RenderComponent rendComp2 = new RenderComponent(terrain, "Textures/terrain");
            AddEntityToScene(terrain);

            World.InitializeWorld(9.87f, 0.47f, 1.168f);

            Console.WriteLine(World.cd + ", " + World.g);

            Entity camera = new Entity(this, "SAM");
            //{X:-395,9691 Y:1000 Z:3367,88}
            camera.position = new Vector3(-680f, 600, 3367f);
            //540,7583 Y:952,196 Z:5820,998}
            //CameraComponent camComp = new CameraComponent(camera); //komponent kamery
            GameInputComponent inputComp = new GameInputComponent(camera); //sterowanie
            //TerrainCollisionComponent terrainCollisionComp = new TerrainCollisionComponent(camera, 1000f);

            rocket = new RocketEntity(this, "CAM", 10, 5000, camera);
            CameraComponent camComp = new CameraComponent(rocket); //komponent kamery
            AddEntityToScene(rocket);
            AddEntityToScene(camera);
            /*Entity opponent = new Entity(this, "PalmTree");
            opponent.position = new Vector3(-340, 0, -300);
            RenderComponent rendComp3 = new RenderComponent(opponent, "Models/palm");

            Entity opponent1 = new Entity(this, "House");
            opponent1.position = new Vector3(-700, -1050, -300);
            RenderComponent rendComp4 = new RenderComponent(opponent1, "Models/house");

            Entity opponent2 = new Entity(this, "TRex");
            opponent2.rotation = Matrix.CreateRotationY(MathHelper.ToRadians(110f));
            opponent2.position = new Vector3(-1800, 0, -1700);
            RenderComponent rendComp5 = new RenderComponent(opponent2, "Models/trex");

            
            TerrainCollisionComponent terrainCollisionComp2 = new TerrainCollisionComponent(opponent, 128f);
            TerrainCollisionComponent terrainCollisionComp3 = new TerrainCollisionComponent(opponent2, 1f);
            
            
            //AddEntityToScene(arcann);
            AddEntityToScene(opponent);
            AddEntityToScene(opponent1);
            AddEntityToScene(opponent2);*/

            heightMapInfo = rendComp2.Description.model.Tag as HeightMapInfo;
            if (heightMapInfo == null)
            {
                string message = "Obiekt nie posiada informacji o mapie wysokosci.";
                throw new InvalidOperationException(message);
            }
            heightMaps.Add(heightMapInfo);
        }

        public override void Update(GameTime gameTime)
        {
            foreach (KeyValuePair<String, Entity> pair in entities)
            {
                //input.moteObject(GamePad.GetState(PlayerIndex.One), Keyboard.GetState(), this.GetEntity(EngineCommon.RootEntityName));
                (pair.Value).Update(gameTime);
            }
        }

        public void AddEntityToScene(Entity entity)
        {
            Entity checkEntity = null;
            if (entities.TryGetValue(entity.Name, out checkEntity))
            {
                throw new Exception("An entity named " + entity.Name + " already exists.");
            }
            entities.Add(entity.Name, entity);
        }

        public void RemoveEntityFromSCene(Entity entity)
        {
            Entity checkEntity = null;
            if (!entities.TryGetValue(entity.Name, out checkEntity))
            {
                throw new Exception("No entity named " + entity.Name + " exist in the scene to be removed.");
            }
            entities.Remove(entity.Name);
        }

        public Entity GetEntity(String entityName)
        {
            Entity entity = null;
            if (!Entities.TryGetValue(entityName, out entity))
            {
            }
            return entity;
        }
    }
}
